import { Module, MiddlewareConsumer, NestModule } from '@nestjs/common';
import {TypeOrmModule} from '@nestjs/typeorm';
import {ConfigModule} from '@nestjs/config';
import {ConfigService} from '@nestjs/config';
import { AboutModule } from './api/about/about.module';
import { TagModule } from './api/tag/tag.module';
import { AccountModule } from './api/account/account.module';
import { JwtAuthGuard } from './global/guard/jwt-auth.guard';
import { APP_GUARD } from '@nestjs/core';
import { JwtMiddleware } from './middlewares/jwt.middleware';
import { JwtStrategy } from 'src/global/strategy/jwt.strategy';
import { BlogModule } from 'src/api/blog/blog.module';
import { MinioModule } from './api/minio/minio.module';
import { AlbumModule } from './api/album/album.module';
import { QiniuModule } from './api/qiniu/qiniu.module';

// @ts-ignore
@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: `.env.${process.env.NODE_ENV || 'development'}`,
      isGlobal: true
    }),
    TypeOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: '119.29.25.241', // 默认值：localhost
        // port: 3306, // 默认值：3306
        username: 'blog', // 默认值：root
        password: 'FbKEiwteem3xKLDj', // 默认值：password
        database: 'blog', // 默认值：mydb
        entities: [__dirname + '/**/**/*.entity{.ts,.js}'],
        retryDelay: 500,
        retryAttempts: 10,
        autoLoadEntities: true,
      }),
      inject: [ConfigService], // 注入 ConfigService 依赖
    }),
    AboutModule,
    AccountModule,
    TagModule,
    BlogModule,
    MinioModule,
    AlbumModule,
    QiniuModule
  ],
  controllers: [],
  providers: [{
    provide: APP_GUARD,
    useClass: JwtAuthGuard,
  }],
})

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(JwtMiddleware).forRoutes('/')
  }
}
